package com.company;

import java.util.HashSet;
import java.util.Set;

/**
 * @date : 2023-09-14 下午4:08
 */

public class P065_HashSet {

    public static void main(String[] args) {
        // 使用数组，new 开辟新地址空间，HashSet 计算地址的Hash，不包含
        arr();
        // 使用Info，重新hashcode方法，HashSet 计算HashCode值，重复则包含
        info();
    }

    private static void arr() {
        Set<int[]> set = new HashSet<>();
        int[][] queens = new int[][]{{0, 1}, {1, 0}, {4, 0}, {0, 4}, {3, 3}, {2, 4}};
        for (int[] queen : queens) {
            set.add(new int[]{queen[0], queen[1]});
        }
        System.out.println(set.contains(new int[]{0, 1}));
    }

    private static void info() {
        Set<Info> set = new HashSet<>();
        int[][] queens = new int[][]{{0, 1}, {1, 0}, {4, 0}, {0, 4}, {3, 3}, {2, 4}};
        for (int[] queen : queens) {
            set.add(new Info(queen[0], queen[1]));
        }
        System.out.println(set.contains(new Info(0, 1)));
    }

    public static class Info {
        int a;
        int b;

        public Info(int a, int b) {
            this.a = a;
            this.b = b;
        }

        @Override
        public boolean equals(Object o) {
            if (this == o) return true;
            if (!(o instanceof Info)) return false;

            Info info = (Info) o;

            if (a != info.a) return false;
            return b == info.b;
        }

        @Override
        public int hashCode() {
            int result = a;
            result = 31 * result + b;
            return result;
        }
    }
}
